import OpenAI from "openai";
import dotenv from "dotenv";
import http from "http";


dotenv.config();
const client = new OpenAI(
    {apiKey:process.env.OPEN_API_KEY,
    baseURL:process.env.OPEN_BASE_URL});

//链接LLM
const getCompletion = async (prompt) => {
    //用户说的话
     const message = [{role: "user", content: prompt}];
     //chat
     const response = await client.chat.completions.create({
        model: 'openai/gpt-4o',
        messages: message,
        temperature:0.1
    });
    return response.choices[0].message.content;
}



//跟ai的交互
const main = async(message) => {
        const user_message = message
        const prompt = `请帮我翻译以下的文字到英文，只需要给出英文单词，"${user_message[0]}"`
        const result = await getCompletion(prompt); 
        return result

}
const server = http.createServer(async(req, res) => {
    res.writeHead(200, { "access-control-allow-origin": "*" });

    //获取到前端的参数
    const query = new URL(req.url,`http://${req.headers.host}`).searchParams;
    const inputText = query.get('inputText')
    const message = [
        inputText
    ]
    const result = await main(message)
    res.end(result)

    // res.end("Hello World!");
});

server.listen(3000,() => {
    console.log("server is running on port 3000");
    
})